<style rel="stylesheet/scss" lang="scss" scoped>
</style>

<template>
    <div>
        <div class="container">
            <el-card shadow="hover" style="padding:0">

                <!-- 操作按钮 -->
                <el-card class="operate_container" shadow="never">
                    <div class="wrapper">
                        <div><i class="el-icon-tickets" style="margin-right: 10px;"></i>数据列表</div>
                        <div>
                            <el-button type="danger" icon="el-icon-delete" size="medium" circle @click="handleDelete"></el-button>
                            <el-button type="primary" icon="el-icon-refresh" size="medium" circle @click="handleResetSearch('searchForm')"></el-button>
                            <el-button type="primary" icon="el-icon-search" size="medium" circle @click="searchVisible = !searchVisible"></el-button>
                        </div>
                    </div>
                </el-card>

                <!-- 条件过滤 -->
                <el-card class="filter_container" shadow="never" v-show="searchVisible">
                    <div style="margin-top: 15px">
                        <el-form ref="searchForm" :inline="true" :model="searchForm" size="medium" label-width="120px">
                            <el-form-item label="日志类型" prop="type">
                                <el-input v-model="searchForm.type" clearable placeholder="日志类型"></el-input>
                            </el-form-item>
                            <el-form-item label="日志标题" prop="title">
                                <el-input v-model="searchForm.title" clearable placeholder="日志标题"></el-input>
                            </el-form-item>
                            <el-form-item label="操作用户" prop="username">
                                <el-input v-model="searchForm.username" clearable placeholder="操作用户"></el-input>
                            </el-form-item>
                            <el-form-item label="操作IP地址" prop="remoteAddr">
                                <el-input v-model="searchForm.remoteAddr" clearable placeholder="操作IP地址"></el-input>
                            </el-form-item>
                            <el-form-item label=" ">
                                <el-button type="primary" @click="handleSearch()" size="medium">搜索</el-button>
                                <el-button @click="handleResetSearch('searchForm')" size="medium">重置</el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                </el-card>

                <!-- 表格 -->
                <el-table
                    ref="table" size="small" empty-text="暂无数据"
                    element-loading-text="给我一点时间"
                    fit stripe border highlight-current-row
                    v-loading="dataListLoading"
                    :data="dataList"
                    :header-cell-style="{'padding-top': '10px', 'height': '50px', 'color': '#515A6E', 'background-color': '#f8f8f9'}"
                    @selection-change="handleSelection">
                    <el-table-column type="selection" width="55" align="center"></el-table-column>
                    <el-table-column type="index" width="55" align="center"></el-table-column>

                    <!-- <el-table-column label="日志类型" prop="type" align="center"/> -->
                    <el-table-column label="日志标题" prop="title" align="center"/>
                    <el-table-column label="服务ID" prop="serviceId" align="center"/>
                    <el-table-column label="操作用户" prop="username" align="center"/>
                    <el-table-column label="操作IP地址" prop="remoteAddr" align="center"/>
                    <el-table-column label="用户代理" prop="userAgent" align="center" show-overflow-tooltip/>
                    <el-table-column label="请求URI" prop="requestUri" align="center" show-overflow-tooltip/>
                    <el-table-column label="操作方式" prop="method" align="center"/>
                    <el-table-column label="操作提交的数据" prop="params" align="center" show-overflow-tooltip/>
                    <el-table-column label="执行时间" prop="time" align="center" show-overflow-tooltip/>
                    <el-table-column label="异常信息" prop="exception" align="center"/>
                    <el-table-column label="创建时间" prop="ctime" align="center" width="150"/>
                    <el-table-column label="更新时间" prop="utime" align="center" width="150"/>

                    <el-table-column label="操作" width="120px" align="center" fixed="right">
                        <template slot-scope="scope">
                            <div class="table_action">
                                <el-button type="danger" icon="el-icon-delete" circle size="small" @click="handleDelete(scope.row)"></el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>

                <!-- 分页工具栏 -->
                <div class="pagination">
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="searchForm.page"
                            :page-sizes="[10, 20, 30, 50, 100, 200]"
                            :page-size="searchForm.limit"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="totalPage">
                    </el-pagination>
                </div>
                
            </el-card>
        </div>
    </div>
</template>

<script>
import { save, deleted, updated, info, list, page, selectMyPage } from "@/api/admin/syslog"

export default {
    data() {
        return {
            visible: false,
            searchVisible: false,
            submitLoading: false,

            dataList: [],
            totalPage: 0,
            dataListLoading: true,

            searchForm: {
                page: 1,
                limit: 10,
                sortColumns: 'ctime',
                sortType: 'desc',
                type: null,
                title: null,
                serviceId: null,
                username: null,
                remoteAddr: null,
                userAgent: null,
                requestUri: null,
                method: null,
                params: null,
                time: null,
                exception: null,
                ctime: null,
                utime: null
            },
            form: {
                id: null,
                type: null,
                title: null,
                serviceId: null,
                username: null,
                remoteAddr: null,
                userAgent: null,
                requestUri: null,
                method: null,
                params: null,
                time: null,
                exception: null,
                ctime: null,
                utime: null
            },
            rules: {
            }
        }
    },
    mounted() {
    	this.$nextTick(function () {
	        this.getDataList()
    	})
    },
    methods: {
        handleSelection(selection) { // 多选
            this.selectionRows = selection
        },
        handleSizeChange(val) { // 每页数量
            this.searchForm.limit = val
            this.getDataList()
        },
        handleCurrentChange(val) { // 页码
            this.searchForm.page = val
            this.getDataList()
        },
        handleResetSearch(name) { // 重置搜索表单
            this.handleReset(name)
            this.getDataList()
        },
        handleReset(name) { // 重置表单
            const refs = this.$refs
            refs[name].resetFields()
        },
        handleSearch() { // 单条件搜索
            this.getDataList()
        },

		/**
         * 删除
         */
        handleDelete(row) { 
            let ids = []
            if (row.id != undefined) {
                ids.push(row.id)
            }
            if(this.selectionRows) {
                this.selectionRows.filter((item, index) => {
                    ids.push(item.id)
                })
            }
            if (ids.length <= 0) {
                this.$notify({ title: '提示', message: '请选择需要删除的记录！', type: 'info' });
            } else {    
                this.$confirm('确定要删除？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
                    deleted({ idList: ids }).then(res => {
                        if (this.ResultMessage(res)) {
                            this.getDataList()
                        } 
                    }).catch(error => {
                        this.$notify({ title: '错误', message: error, type: 'error' });
                    })
                }).catch(() => {})
            }
        },

		/**
         * 详细信息
         */
        getInfo(id) {
            if (id !== undefined) {
                return info(id).then(res => {
                    if (this.ResultMessage(res, false)) {
                        return res
                    } else {
                        this.$notify({ title: '错误', message: error, type: 'error' });
                    }
                }).catch(error => {
                    this.$notify({ title: '错误', message: error, type: 'error' });
                })
            }
        },
		
        /**
         * 数据列表
         */
        getDataList() {
            page(this.searchForm).then(res => {
                if (this.ResultMessage(res, false)) {
                    this.dataList = res.data.records
                    this.totalPage = res.data.total
                    this.dataListLoading = false
                } else {
                    this.dataListLoading = false
                }
            }).catch(error => {
                this.dataListLoading = false
                this.$notify({ title: '错误', message: error, type: 'error' });
            })
        }
    }
}
</script>